<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="CustomModWatcher.aspx.cs" Inherits="ModWatcher_CustomModWatcher"
	Title="CustomModWatcher - ModWatch" %>

<asp:Content ContentPlaceHolderID="Head" runat="server">
	<script language="javascript">
		function checkForClientChange(sender, e)
		{
			var savedVal = parseInt(e.get_savedValue());
			var currentVal = parseInt($get('<%= txtInput.ClientID %>').value);
			
			if (isNaN(savedVal))
			{
				if (!isNaN(currentVal))
					e.set_hasChanged(true);
			}
			else if (savedVal != currentVal)
			{
				e.set_hasChanged(true);
			}
		}
	</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="PageContent" runat="server">

	<h2>CustomModWatcher Control</h2>

	<p>
		The CustomModWatcher control tracks changes
		in a custom way defined by the developer.
		The CustomModWatcher requires the developer to define
		certain functionality both on the client and on the server.
	</p>

	<p>
		This sample page will define a CustomModWatcher control that
		converts the value of a TextBox to an integer, and determines if it
		has changed.  Thus a value of '1' and '01' will be considered equal.
	</p>

	<p>
		To start, we will define a TextBox and a CustomModWatcher control:
	</p>

	<p>
		Enter an integer: <br />
		&lt;asp:TextBox ID="txtInput" runat="server">&lt;/asp:TextBox> <br />
		&lt;mw:CustomModWatcher ID="cmwIntModWatcher" runat="server" <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			ClientCheckChange="checkForClientChange" <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			OnSavingChanges="cmwIntModWatcher_SavingChanges" <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			OnCheckingChange="cmwIntModWatcher_CheckingChange" />
	</p>

	<p>
		The CustomModWatcher control contains a ClientCheckChange property that
		specifies a javascript function that determines whether changes have been
		made on the client.  This function takes two parameters.  The first is the
		CustomModWatcher control for which the evaluation is being made.  The second
		is a CustomModWatcherCheckChangeEventArgs object that defines two properties:
		the readonly savedValue property retrieves the value that was most recently
		saved; the read/write hasChanged property gets or sets a value indicating
		whether or not the ModWatch framework should be notified that a change exists.
	</p>

	<p>
		function checkForClientChange(sender, e) <br />
		{ <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			var savedVal = parseInt(e.get_savedValue()); <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			var currentVal = parseInt($get('<%= txtInput.ClientID %>').value); <br /><br />

			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			if (isNaN(savedVal)) <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			{ <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				if (!isNaN(currentVal)) <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					e.set_hasChanged(true); <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			} <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			else if (savedVal != currentVal) <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			{ <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				e.set_hasChanged(true); <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			} <br />
		}
	</p>

	<p>
		The CustomModWatcher control also requires two server side events to
		be handled: SavingChanges and CheckingChange.
	</p>
	<p>
		The SavingChanges event takes a CustomModWatcherSaveEventArgs
		object as an argument.  This argument defines a single property: SavedValue.
		The event handler for this event should set the SavedValue property to the
		current state for the CustomModWatcher.
	</p>
	<p>
		The CheckingChange event takes a CustomModWatcherCheckChangeEventArgs
		object as an argument.  This argument defines two properties:
		the readonly SavedValue property retrieves the value that was most recently
		saved; the read/write HasChanged property gets or sets a value indicating
		whether or not the ModWatch framework should be notified that a change exists.
	</p>
	
	<p>
		protected void cmwIntModWatcher_SavingChanges <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			(object sender, CustomModWatcherSaveEventArgs e) <br />
		{ <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			int currentValue; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			if (int.TryParse(txtInput.Text, out currentValue)) <br />
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				e.SavedValue = currentValue; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			else <br />
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				e.SavedValue = null; <br />
		} <br />

		<br />
 
		protected void cmwIntModWatcher_CheckingChange <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			(object sender, CustomModWatcherCheckChangeEventArgs e) <br />
		{ <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			int? savedValue = (int?)e.SavedValue; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			int currentValue; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			if (int.TryParse(txtInput.Text, out currentValue)) <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			{ <br />
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				if (currentValue != savedValue) <br />
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					e.HasChanged = true; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			} <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			else if (savedValue != null) <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			{ <br />
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				e.HasChanged = true; <br />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			} <br />
		}
	</p>

	<p>
		Enter an integer:
		<asp:TextBox ID="txtInput" runat="server"></asp:TextBox>
		<mw:CustomModWatcher ID="cmwIntModWatcher" runat="server"
			ClientCheckChange="checkForClientChange"
			OnSavingChanges="cmwIntModWatcher_SavingChanges"
			OnCheckingChange="cmwIntModWatcher_CheckingChange" />

		<br /><br />

		<asp:Button Text="Fire ModAlert" ID="btnTest" runat="server" />
		<mw:OnClickModAlertExtender TargetControlID="btnTest" runat="server"
			NotifyModMessage="Changes have been made, are you sure you want to continue?" />

		<asp:Button Text="Save Changes" OnClick="btnSave_Click" ID="btnSave" runat="server" />

		<br /><br />
		
		Page last served: <asp:Label ID="lblLastServed" runat="server"></asp:Label>
		<br />
		Change detected on server? <asp:Label ID="lblServerChange" runat="server"></asp:Label>
	</p>

</asp:Content>
